package com.incross.itracker.tracking;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.incross.itracker.util.BKLog;
import com.incross.itracker.util.CommonUtils;


public class DeviceInformation {
	private String mDeviceName = ""; // Model name
	private String mUdid = ""; // Device unique id
	private String mProvider = ""; // network provider
	private String mCurrentLanguage = ""; // current language
	private String mIP = ""; // current IP
	private String mCountryCode = "";
	
	private Context mContext;
	
	public DeviceInformation(Context context) {
		mContext = context;
		getDeviceInfo();
	}
	
	private void getDeviceInfo() {
		TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		
		mDeviceName = android.os.Build.MODEL;
        mUdid = telephonyManager.getDeviceId();
        mProvider = telephonyManager.getSimOperatorName();
		mCurrentLanguage = mContext.getResources().getConfiguration().locale.getLanguage();
		// 20140526 국가코드 수집에 대한 협의내용.
		// Sim Operator 기준으로 국가 코드를 가져오되 Sim card를 찾을 수 없는 모델의 경우 locale.getCountry로 언어에 따른 기본값을 가지고 오는 것으로 협의함(문주원, 구현모) 
		BKLog.e("Sim state : " + telephonyManager.getSimState());
		if(telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY) {
			// SIM Card state가 정상인 경우 SIM Card Operator의 국가를 추출함.
			mCountryCode = telephonyManager.getSimCountryIso();
		} else {
			// SIM Card가 준비된 경우를 제외한 나머지 경우 System locale을 가지고 오도록 설정함.
			mCountryCode = Locale.getDefault().getCountry();
		}
		
		Log.e("BenTag", "Device name : " + mDeviceName + ", UDID : " + mUdid + ", Provider : " + mProvider + 
			", Language : " + mCurrentLanguage + ", Country code : " + mCountryCode + ", Android Version : " + getAndroidVersion());
		
	}
	
	
	public String getDeviceName() {
		return (mDeviceName == null? "": mDeviceName);
	}
	
	public String getUdid() {
		return (mUdid == null? "": mUdid);
	}
	
	public String getNetworkState() {
		int networkState = CommonUtils.getCurrentNetworkState(mContext);
		switch(networkState) {
			case CommonUtils.NETWORK_STATE_WIFI:
				return "WIFI";
			case CommonUtils.NETWORK_STATE_MOBILE_NETWORK:
				return "3G+LTE";
			case CommonUtils.NETWORK_STATE_UNKNOWN:
				return "UNKNOWN";
		}
		return "UNKNOWN";
	}
	
	public String getProvider() {
		return ((mProvider == null || mProvider.equals(""))? "UNKNOWN": mProvider);
	}
	
	public String getIP() {
		mIP = CommonUtils.getIPAddress(true);
		return ((mIP == null || mIP.equals(""))? "UNKNOWN": mIP);
	}
	
	public String getAndroidVersion() {
		return (Build.VERSION.RELEASE == null? "UNKNOWN": Build.VERSION.RELEASE);
	}
	
	public String getCountryCode() {
		return ((mCountryCode == null || mCountryCode.equals(""))? "UNKNOWN": mCountryCode);
	}

}
